package pojo.classentity;

import pojo.Sqlentity;
import uilt.Scan;

import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class Class extends Sqlentity {
	public static Sqlentity sqlentity=new Sqlentity();
	Scan sc = new Scan();
	public void menu() throws Exception {
		while (true) {
			System.out.println("******欢迎来到班级模块******");
			System.out.println("1.创建班级");
			System.out.println("2.班级列表展示");
			System.out.println("3.修改班级信息");
			System.out.println("4.退       出");
			System.out.print("请输入：");

			int i = sc.getkey();

			if (i == 1) {
				addclass();
			} else if (i == 2) {
				classmenu();
			}else if(i == 3){
				System.out.println("******欢迎来到修改班级信息******");
				System.out.print("请输入您要修改的班级编号：");
				int ci = sc.getkey();
				upclass(ci);
			}
			else {
				break;
			}
		}
	}

	public void classmenu(){
		while (true) {
			System.out.println("******欢迎来到班级列表展示******");
			System.out.println("1.查看班级所有学生");
			System.out.println("2.查看教师所带班级");
			System.out.println("3.添加学生进入班级");
			System.out.println("4.删除班级里的学生");
			System.out.println("5.退       出");

			System.out.print("请输入：");
			int i = sc.getkey();

			if(i == 1){
				System.out.println("******欢迎来到查看班级学生模块*******");
				System.out.print("请输入想要查看班级的编号:");
				int ci = sc.getkey();
				showstudents(ci);
			}
			else if(i == 2){
				System.out.println("******欢迎来到查看老师所带班级模块******");
				System.out.print("请输入教师编号：");
				int ci = sc.getkey();
				showclass(ci);
			}
			else if(i == 3){
				System.out.println("******欢迎来到添加学生模块******");
				System.out.print("请输入你想要添加的学生编号：");
				int si = sc.getkey();
				System.out.print("请输入该学生所在的班级编号：");
				int ci = sc.getkey();
				intoclass(si,ci);
			}
			else if (i == 4){
				System.out.println("******欢迎来到删除学生模块******");
				System.out.print("请输入你想要删除的学生编号：");
				int si = sc.getkey();
				System.out.println("请输入该学生所在的班级编号：");
				int ci = sc.getkey();
				declass(si,ci);
			}
			else{
				break;
			}
		}
	}

	public static void addclass() throws Exception  {
		//新建班级
		System.out.println("******欢迎来到创建班级******");
		System.out.println("请依次输入 班级名 班级地点 班级人数 (逗号隔开)");
		String output = Scan.getString();
		String[] s = output.split(",");
		String sql = "insert into class(classname,classplace,classstate,classpeople,classteacherid) values(?,?,1,?,123)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, s[0]);
		ps.setString(2, s[1]);
		ps.setInt(3, Integer.parseInt(s[2]));
		int i = ps.executeUpdate();
		if (i != -1) {
			System.out.println("添加成功");
		} else {
			System.out.println("添加失败");
		}
	}

	public static void showstudents(int classid){
		//查看班里所有学生
		String sql="select * from student where classid="+classid;
		try {
			ResultSet res =sqlentity.select(sql);
			while (res.next()){
				System.out.println("学生id为："+res.getString("stuid")+"，学生姓名为："+res.getString("stuname"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void showclass(int classteacherid){
		//查看教师所有班级
		String sql="select * from class where classteacherid="+classteacherid;
		try {
			ResultSet res =sqlentity.select(sql);
			while (res.next()){
				System.out.println("班级编号为："+res.getString("classid")+"\t班级名称为："+res.getString("classname"));

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void intoclass(int studentid,int classid){
		//添加学生进入班级
		String sql="update student set classid="+classid+" where stuid="+studentid;
		System.out.println(sql);
		try {
			sqlentity.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void declass(int studentid,int classid){
		//在班级里删除学生
		String sql="update student set classid="+null+" where studentid="+studentid;
		try {
			sqlentity.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void upclass(int classid) throws Exception{
		//修改班级信息
		String sql="update class set classname=?,classplace=?,classpeople=?,classstate=?,classteacherid=? where classid="+classid;
		System.out.println(sql);
		System.out.println("请依次输入修改后的 班级名 班级地点 班级人数 班级状态 班主任号(-隔开)");
		String output = Scan.getString();
		String[] s = output.split("-");

		PreparedStatement ps = connection.prepareStatement(sql);
		try{

			ps.setString(1, s[0]);
			ps.setString(2, s[1]);
			ps.setInt(3,Integer.valueOf(s[2]));
			ps.setInt(4,Integer.valueOf(s[3]));
			ps.setInt(5,Integer.valueOf(s[4]));

		}catch (Exception E){
			E.printStackTrace();
		}

		int i = ps.executeUpdate();
		if (i != -1) {
			System.out.println("操作成功");
		} else {
			System.out.println("操作失败");
		}

	}

}
